package com.hgd.security.evp.common.context;

import com.hgd.security.evp.common.constances.CommonConstants;
import com.hgd.security.evp.common.util.EmptyUtil;

import java.util.HashMap;
import java.util.Map;

public class CurrentContext {
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    private static void initThreadLocalMap(){
        if(EmptyUtil.isEmpty(threadLocal.get())){
            threadLocal.set(new HashMap<>());
        }
    }

    public CurrentContext() {
    }

    public static void set(Map<String, Object> map) {
        threadLocal.set(map);
    }

    public static Map<String, Object> get() {
        return threadLocal.get();
    }

    public static void setValue(String key, Object value) {
        initThreadLocalMap();
        threadLocal.get().put(key, value);
    }

    public static Object getValue(String key) {
        initThreadLocalMap();
        return threadLocal.get().get(key);
    }

    public static String getUserId() {
        return (String) getValue(CommonConstants.JWT_KEY_USER_ID);
    }

    public static String getUserName() {
        return (String) getValue(CommonConstants.JWT_KEY_NAME);
    }

    public static String getEAccount() {
        return (String) getValue(CommonConstants.JWT_KEY_ACCOUNT);
    }

    public static String getTenantId() {
        return (String) getValue(CommonConstants.JWT_KEY_TENANT_ID);
    }

    public static void setToken(String token) {
        setValue(CommonConstants.CONTEXT_KEY_USER_TOKEN, token);
    }

    public static void setEAccount(String account) {
        setValue(CommonConstants.JWT_KEY_ACCOUNT, account);
    }

    public static void setUserName(String name) {
        setValue(CommonConstants.JWT_KEY_NAME, name);
    }

    public static void setUserId(String id) {
        setValue(CommonConstants.JWT_KEY_USER_ID, id);
    }

    public static void setDeptId(String deptId) {
        setValue(CommonConstants.JWT_KEY_DEPART_ID, deptId);
    }

    public static String getDeptId(){
        return (String)getValue(CommonConstants.JWT_KEY_DEPART_ID);
    }

    public static void setTenantId(String tenantId) {
        setValue(CommonConstants.JWT_KEY_TENANT_ID, tenantId);
    }

    public static void remove() {
        threadLocal.remove();
    }
}
